<template>
  <div>
    <div class="flex flex-col h-full">
      <SearchContainer @search="queryList" @reset="() => (formState = {})">
        <a-form :model="formState" class="form-style-flex">
          <a-form-item name="zjm" class="w-1/3">
            <a-input v-model:value="formState.zjm" placeholder="手术代码/手术名称/助记码" />
          </a-form-item>
        </a-form>
      </SearchContainer>
      <a-card class="mt-2 flex-1">
        <div class="mb-3">
          <BaseButton type="primary" confirm-text="确定全部停用?" @click="onHandleDeactivate({}, 0)">全部停用</BaseButton>
          <!-- <BaseButton type="primary" class="ml-2" confirm-text="确定全部启用?" @click="onHandleDeactivate({}, 0)">全部启用</BaseButton> -->
        </div>
        <BaseTable
          :loading="loading"
          column-code="00001195"
          fill
          :data-source="tableData"
          bordered
          :pagination="{ total: page.total, current: page.current, pageSize: page.pageSize }"
          @change="onPageChange"
        >
          <template #bodyCell="{ column, index, record }">
            <template v-if="column.dataIndex === 'xh'">
              {{ index + 1 }}
            </template>
            <template v-if="column.dataIndex === 'status'">
              <a-switch v-model:checked="record.status" checked-value="0" un-checked-value="1" @change="onHandleDeactivate(record, $event)" />
            </template>
            <template v-if="column.dataIndex === 'releaseFlag'">
              <a-tag v-if="record.releaseFlag === '1'" color="green">已发布</a-tag>
              <a-tag v-else color="red">未发布</a-tag>
            </template>
          </template>
        </BaseTable>
      </a-card>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'

const loading = ref(false)
const tableData = ref([])
const page = ref({ total: 0, current: 1, pageSize: getSystemConfig('DFFAULT_PAGE_SHOW_COUNT') || 10 })

const formState = ref({})
const onHandleDeactivate = async (record, number) => {
  const { code, msg } = await post('/operation/opex/doUpdateICD9Status', { ...record, status: number })
  if (code !== 0) return message.error(msg)

  message.success(msg)
  queryList()
}

const queryList = async () => {
  loading.value = true
  // formState.value
  const { code, data, total } = await post('/common/masterData/getDictICD9List', {
    zjm: formState.value.zjm,
    pageNum: page.value.current,
    pageSize: page.value.pageSize
  })
  if (code !== 0) return
  tableData.value = data.map((item) => {
    item.status = item.status ? '1' : '0'
    return item
  })
  loading.value = false
  page.value.total = total
}
const onPageChange = ({ current, pageSize }) => {
  page.value.current = current
  page.value.pageSize = pageSize
  queryList()
}
onMounted(() => {
  queryList()
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
